﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;

namespace TMDT.Web.UI.Controls
{
    public partial class SanPhamControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }

        }

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            var service = new SanPhamManager();
            rptSanPham.DataSource = service.GetSanPhamByIDNguoiBan(new TaiKhoanManager().GetIDByTenDangNhap(Session["User"].ToString()).IDTaiKhoan);
            rptSanPham.DataBind();
        }

        #region Event

        protected void rptSanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var sanPham = (BusinessObjects.SanPham) e.Item.DataItem;
            var loaiSanPham = new LoaiSanPhamManager();
            var nhaSanXuat = new NhaSanXuatManager();

            var lblLoaiSanPham = e.Item.FindControl("lblLoaiSanPham") as Label;
            var lblNhaSanXuat = e.Item.FindControl("lblNhaSanXuat") as Label;
            var ltrKhuyenMai = e.Item.FindControl("ltrKhuyenMai") as Literal;

            lblLoaiSanPham.Text = loaiSanPham.GetLoaiSanPham(sanPham.IDLoaiSanPham).TenLoaiSanPham;
            lblNhaSanXuat.Text = nhaSanXuat.GetNhaSanXuat(sanPham.IDNhaSanXuat).TenNhaSanXuat;
            ltrKhuyenMai.Text = sanPham.IsKhuyenMai?"<img src='Images/check.png' alt =''":"";
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton) sender).CommandArgument;
            var spService = new SanPhamManager();
            var haService = new HinhAnhManager();
            var ndspService = new NoiDungSanPhamManager();

            ndspService.Remove(new Guid(id));
            haService.Remove(id);
            spService.Remove(new Guid(id));
            LoadData();
        }

        #endregion
    }
}